package com.atguigu.daijia.customer.controller;

import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.customer.service.CustomerInfoService;
import com.atguigu.daijia.model.entity.customer.CustomerInfo;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@Slf4j
@RestController
@RequestMapping("/customer/info")
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerInfoController {

	@Autowired
	private CustomerInfoService customerInfoService;

	@Operation(summary = "获取客户基本信息")
	@GetMapping("/getCustomerInfo/{customerId}")
	public Result<CustomerInfo> getCustomerInfo(@PathVariable Long customerId) {
		return Result.ok(customerInfoService.getById(customerId));
	}


	//微信小程序登录接口

	/**
	 *
	 * @param code 前端调用微信接口返回的code
	 * @return 用户id
	 */
	@Operation(summary = "小程序授权登录")
	@GetMapping("/login/{code}")
	public Result<Long> login(@PathVariable String code) {
		return Result.ok(customerInfoService.login(code));
	}

	/**
	 * 查询用户登录信息
	 * @param userId 用户id
	 * @return
	 */
	@Operation(summary = "查询用户登录信息")
	@GetMapping("/getCustomerLoginInfo")
	public Result<CustomerLoginVo> getCustomerLoginVo(@RequestParam("userId") Long userId){
		if (!Optional.ofNullable(userId).isPresent()) {
			throw new GuiguException(ResultCodeEnum.DATA_ERROR);
		}
		return Result.ok(customerInfoService.getLoginVo(userId));
	}
	@PostMapping("/updateWxPhone")
	public Result updateWxPhone(@RequestBody UpdateWxPhoneForm updateWxPhoneForm){
		customerInfoService.updateWxPhoneByCode(updateWxPhoneForm);
		return Result.ok();
	}

}

